<?php

class ActionChangePassword extends Action {
	public function IsPermitted(){ return !User::GetCurrent()->IsVisitor(); }
	public function GetIconName(){ return 'oxy/ico/Password'; }
	
	public function Render(){

		$v = new ValidatorSet();		
		if ($this->IsPostback()){
			$o = Http::$POST['OldPassword']->AsString();
			$v->OldPassword->CheckMandatory($o);
			if ($v->HasPassed()){
				$v->OldPassword->Check(Oxygen::Hash($o) == User::GetCurrent()->Password,new ErrorMessage(Lemma::ErrorInvalidPassword()));
			}
			
			$n = Http::$POST['NewPassword']->AsString();
			$v->NewPassword->CheckMandatory($n);

			$c = Http::$POST['ConfirmPassword']->AsString();
			if ($v->ConfirmPassword->CheckMandatory($c));
			if ($v->ConfirmPassword->HasPassed()){
				$v->ConfirmPassword->Check($n == $c, Lemma::ErrorPasswordsDoNotMatch());
			}


			if ($v->HasPassed()){
				User::GetCurrent()->Password = Oxygen::Hash($n);
				User::GetCurrent()->Save();
				echo '<br/>';
				echo new SuccessMessage(Lemma::MessagePasswordChanged());
				return;
			}
		}
		
		echo '<br/>';
		echo $this->GetForm();
		$z = FieldTableControl::Make();
		$z->Add(Lemma::OldPassword())->Write(PasswordControl::Make('OldPassword')->WithWidth('250px'))->WithValidator($v->OldPassword);
		$z->Add(Lemma::NewPassword())->Write(PasswordControl::Make('NewPassword')->WithWidth('250px'))->WithValidator($v->NewPassword);
		$z->Add(Lemma::ConfirmPassword())->Write(PasswordControl::Make('ConfirmPassword')->WithWidth('250px'))->WithValidator($v->ConfirmPassword);
		$z->Add(ButtonControl::Make()->WithIsSubmit(true)->WithValue(Lemma::OK()));
		$z->Render();
		echo $this->EndForm();
		
	}
	
	
}

?>
